package repositories;

import java.sql.SQLException;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import books.BookInfo;

/**
 */
public class BookInfoRepository extends HibernateGenericDAO<BookInfo> implements GenericRepository<BookInfo> {

    private static final long serialVersionUID = 1L;

    @Override
    protected Class<BookInfo> getDomainClass() {
        return BookInfo.class;
    }

    public BookInfo findBookByIsbn(final String isbn) {
        return (BookInfo) this.getHibernateTemplate().execute(new HibernateCallback<Object>() {
            @Override
            public BookInfo doInHibernate(final Session session) throws HibernateException, SQLException {
                Criteria criteria = session.createCriteria(BookInfo.class);
                criteria.add(Restrictions.eq("isbn", isbn));
                return (BookInfo) criteria.uniqueResult();
            }
        });
    }
}
